home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CICA 1995 August
/
CICA - The Ultimate Collection of Shareware for Windows (Disc 2) (August 1995).iso
/
disc2
/
programr
/
stdg44.exe
/
lha
/
EVENTS2.C
< prev
next >
Wrap
C/C++ Source or Header
|
1994-01-10
|
4KB
|
168 lines
#include <stdio.h>
#include <string.h>
#include "stdg.h"
/*
* This program displays mouse, keyboard, resize and close events
* as they happen. It uses an offscreen bitmap which allows redrawing
* of the window when it is resized.
*/
short enabled = Enabled;
menuitem file_menu[] = {
{ "File", 0, &enabled, NULL },
{ "Quit", 'Q', &enabled, &gexit },
{ NULL, 0, NULL, NULL }
};
menu menu_bar[] = {
file_menu, NULL
};
char buf[256];
void scrollup(bitmap *b, long h) /* scroll a bitmap upwards h pixels */
{
rectangle sr = b->r; /* source rectangle */
point dp = b->r.min; /* destination point */
sr.min.y += h;
bit_copy(b, dp, b, sr, S);
sr.min.y = sr.max.y - h;
fill_rect(b, sr, WHITE); /* erase last line of text */
}
void drawstr(window *w, char *str) /* draw string at bottom of window */
{
point dp;
bitmap *b;
b = (bitmap *) w->data; /* draw into offscreen bitmap */
if (b == NULL) /* error - so use window bitmap */
b = w->b;
scrollup(b, fixed_font->height);
dp = pt(3, b->r.max.y - fixed_font->height);
draw_string(b, dp, fixed_font, str, BLACK);
}
void showbitmap(window *w) /* show offscreen bitmap on window */
{
bitmap *b;
rectangle sr;
b = (bitmap *) w->data;
if (b != NULL) {
sr = b->r;
sr.min.y = sr.max.y - w->b->r.max.y; /* show bitmap bottom */
bit_copy(w->b, pt(0,0), b, sr, S);
}
}
void close(window *w) /* handle click in close box */
{
sprintf(buf, "Close: Attempt to close the window with close box.");
drawstr(w, buf);
showbitmap(w);
}
void resize(window *w) /* resize the offscreen bitmap */
{
if (w->data == NULL)
w->data = new_bitmap(screen->r, 1);
sprintf(buf, "Resize: r=(%ld,%ld,%ld,%ld)", w->r);
drawstr(w, buf);
}
void redraw(window *w) /* redraw window from offscreen */
{
showbitmap(w);
}
void printchar(window *w, ushort c) /* print out a keyboard char message */
{
if ((c >= 0x0020) && (c <= 0x00FF))
sprintf(buf, "Key: hex=0x%4.4X char='%c'", c, c);
else
sprintf(buf, "Key: hex=0x%4.4X", c);
drawstr(w, buf);
showbitmap(w);
}
void printmouse(window *w, mouse m) /* print out a mouse message */
{
sprintf(buf, "Mouse: xy= (%3ld, %3ld) buttons= ", m.xy.x, m.xy.y);
if (m.buttons & LeftButton)
strcat(buf, "L");
else
strcat(buf, "-");
if (m.buttons & MiddleButton)
strcat(buf, "M");
else
strcat(buf, "-");
if (m.buttons & RightButton)
strcat(buf, "R");
else
strcat(buf, "-");
strcat(buf, " kind= ");
if (m.kind == MouseMove) strcat(buf, "MouseMove");
if (m.kind == MouseTimer) strcat(buf, "MouseTimer");
if (m.kind & DoubleClick) strcat(buf, "DoubleClick | ");
if (m.kind & MouseDown) strcat(buf, "MouseDown | ");
if (m.kind & MouseUp) strcat(buf, "MouseUp | ");
if (m.kind & LeftButton) {
strcat(buf, "LeftButton");
if ((m.kind & MiddleButton) || (m.kind & RightButton))
strcat(buf, " | "); /* put separating OR sign in */
}
if (m.kind & MiddleButton) {
strcat(buf, "MiddleButton");
if (m.kind & RightButton)
strcat(buf, " | "); /* put separating OR sign in */
}
if (m.kind & RightButton)
strcat(buf, "RightButton");
drawstr(w, buf);
showbitmap(w);
}
int main(int argc, char **argv)
{
ushort c;
mouse m;
window *w;
ginit("Sample", NULL, menu_bar);
w = new_window("Event Look", rect(0,0,0,0),
Closebox | Titlebar | Resize | Maximize | DoubleClicks);
set_winfns(w, &close, &resize, &redraw);
show_window(w);
set_mouse_delay(1000);
while(1) {
if (can_mouse(w)) {
m = get_mouse(w);
printmouse(w, m);
}
if (can_key(w)) {
c = get_key(w);
printchar(w, c);
}
}
return 0;
}